"""
Unittest for jena_fuseki.py
@Time: 2020/10/21 16:26
@Author: yb
@File: jena_fuseki_test.py
"""

import unittest

from src.spiders.common.jena_fuseki import FusekiQuery, FusekiUpdate
from src.spiders.common.rdf_prefix import RdfPrefix


class TestFusekiQuery(unittest.TestCase):
    """FusekiQuery 类的单元测试"""

    def setUp(self) -> None:
        self.fuseki = FusekiQuery('http://localhost:3030', 'test_db')

    def test_query(self):
        """测试查询语句的运行"""
        query_str = """
            SELECT ?subject ?predicate ?object
            WHERE {
                ?subject ?predicate ?object
            }
            LIMIT 25
        """
        print(self.fuseki.run_sparql(query_str))


class TestFusekiUpdate(unittest.TestCase):
    """FusekiUpdate 类的单元测试"""

    def setUp(self) -> None:
        self.fuseki = FusekiUpdate('http://localhost:3030', 'test_db')

    def test_insert_one_record(self):
        """测试插入记录"""
        self.fuseki.insert_one_record(
            None,
            '<http://inet.org/tecent>',
            '<http://inet.org/hasApp>',
            '<http://inet.org/Tim>')

    def test_insert_records(self):
        """测试插入多个记录"""
        s = '<http://example/book1>'
        p_o = [('dc:title', '"A new book"'), ('dc:creator', '"A.N.Other"')]
        self.fuseki.insert_records((RdfPrefix.dc, ), s, p_o)


if __name__ == '__main__':
    unittest.main()
